• Home
  • Education
  • Projects
  • Miscellaneous
  • Contact
/content/drive/MyDrive/Colab Notebooks/Explainable-AI

Explainable AI¶

Nutshell¶

In this project I use DataRobot to predict the type of food from images, as explained on the course Modern Artificial Intelligence, lectured by Dr. Ryan Ahmed, Ph.D. MBA. DataRobot is an end-to-end enterprise AI platform that automates and accelerates every step from data to value.

Data¶

The original dataset from https://www.kaggle.com/vermaavi/food11 consists of 16643 color images belonging to 11 categories. Due to data limitations I will use pictures from 4 classes only:

  1. Dessert
  2. Seafood
  3. Fried food
  4. Vegetable-Fruit

Grad-CAM visualization¶

Gradient-Weighted Class Activation Mapping (Grad-CAM) makes it possible to visualize the regions of the input that contributed towards making prediction by the model. It does so by using the class- specific gradient information flowing into the final convolutional layer of CNN to localize the important regions in the image that resulted in predicting that particular class.

Steps¶

  • To visualize the activation maps, first the image has to be passed through the model to make the prediction. Using argmax find the index corresponding to the maximum value in the prediction - this is the predicted class.
  • Next, the gradient that is used to arrive to the predicted class from the feature map activations A^k is calculated.

$$ \alpha = \frac{1}{Z} \sum_i \sum_j \frac{\partial y^c}{\partial A^k_{ij}}$$ where $$\frac{1}{Z} \sum_i = \text{global average pooling}$$ $$\frac{\partial y^c}{\partial A^k_{ij}} = \text{gradients via backdrop}$$

  • To enhance the filter values that resulted in this prediction, the values are multiplied with tensorflow.GradientTape() with the filter values in the last convolutional layer.
  • This enhances the filter values that contributed towards making this prediction and lower the filter values that didn't contribute.
  • Then, the weighted combination of activation maps is performed and followed by ReLU to obtain the heatmap.

$$ L^c_{Grad-CAM}= ReLU(\sum \alpha^c_kA^k) $$ where

Finally, the feature heatmap is super-imposed on the original image to see the activation locations in the image.

The best performing model for this task was Regularized Logistic Regression (L2). $$ \begin{array}{lccc} \hline \textbf{Metric} & \textbf{Validation} & \textbf{Cross-validation} & \textbf{Holdout} \\ \hline \text{AUC} & 0.9788 & 0.9877 & 0.9822 \\ \text{Accuracy} & 0.8900 & 0.9188 & 0.9036 \\ \text{Balanced Accuracy} & 0.8885 & 0.9196 & 0.9045 \\ \text{FVE Multinomial} & 0.7699 & 0.8202 & 0.7815 \\ \text{LogLoss} & 0.3188 & 0.2475 & 0.3019 \\ \hline \end{array} $$

No description has been provided for this image

Below is an example from the DataRobot models attention maps.

No description has been provided for this image

Applying a Grad-CAM for the Brain tumor detector classifier model¶

Next I will implement a Grad-CAM pipeline for the model built in my other project. This model takes MRI images of the brain as input and classifies them intotwo classes: contains a brain tumor or not. You can check the project here: Brain tumor detector

Introduction to the Brain Tumor Detection¶

Deep learning has proven to be as good and even better than humans in detecting diseases from X-rays, MRI scans and CT scans. there is huge potential in using AI to speed up and improve the accuracy of diagnosis. This project will use the labeled dataset from https://www.kaggle.com/datasets/mateuszbuda/lgg-mri-segmentation which consists of 3929 Brain MRI scans and the tumor location. The final pipeline has a two step process where

  1. A Resnet deep learning classifier model will classify the input images into two groups: tumor detected and tumor not detected.
  2. For the images, where tumor was detected, a second step is performed, where a ResUNet segmentation model detects the tumor location on the pixel level.

Below is an exmaple of an MRI image and the matching mask. This example has a small tumor. In images where no tumor is present, the mask will be complety black.

No description has been provided for this image

Convolutional neural networks (CNNs)¶

  • The first CNN layers are used to extract high level general features
  • The last couple of layers will perform classification
  • Locla respective fields scan the image first searching for simple shapes such as edges and lines
  • The edges are picked up by the subsequent layer to form more complex features

A good visualisation of the feature extraction with convolutions can be found at https://setosa.io/ev/image-kernels/

ResNet (Residual Network)¶

  • As CNNs grow deeper, vanishing gradients negatively imapct the network performance. Vanishing gradient occurs when the gradient is backpropagated to earlier layers which results in a very small gradient.
  • ResNets "skip connection" feature can allow training of 152 layers wihtout vanishing gradient problems
  • ResNet adds "identity mapping on top of the CNN
  • ResNet deep network is trained with ImageNet, which contains 11 million images and 11 000 categories

ResNet paper (He etal, 2015): https://arxiv.org/pdf/1512.03385

As seen in the Figure 6. from the Resnet paper, the ResNet architectures overcome the training challenges from deep networks compared ot the plain networks. ResNet-152 achieved 3.58% error rate on the ImageNet dataset. This is better than human performance.

No description has been provided for this image
# @title Preparing image generators
train_generator = datagen.flow_from_dataframe(
    dataframe = train,
    directory = './',
    x_col = 'image_path',
    y_col = 'mask',
    subset = 'training',
    batch_size =16,
    shuffle = True,
    class_mode = 'categorical',
    target_size = (256, 256)
)
valid_generator = datagen.flow_from_dataframe(
    dataframe = train,
    directory = './',
    x_col = 'image_path',
    y_col = 'mask',
    subset = 'validation',
    batch_size = 16,
    shuffle = True,
    class_mode = 'categorical',
    target_size = (256, 256)
)
#create a data generator for test images
#no need for splitting again because here we use the "test" data set
test_datagen = ImageDataGenerator(rescale=1./255)

test_generator = datagen.flow_from_dataframe(
    dataframe = test,
    directory = './',
    x_col = 'image_path',
    y_col = 'mask',
    batch_size = 16,
    shuffle = False,
    class_mode = 'categorical',
    target_size = (256, 256)
)
Found 2839 validated image filenames belonging to 2 classes.
Found 500 validated image filenames belonging to 2 classes.
Found 590 validated image filenames belonging to 2 classes.
37/37 ━━━━━━━━━━━━━━━━━━━━ 142s 4s/step
The model accuracy is 0.98

Assessment of the model¶

No description has been provided for this image
Classification report
              precision    recall  f1-score   support

           0       0.98      0.98      0.98       383
           1       0.97      0.97      0.97       207

   micro avg       0.98      0.98      0.98       590
   macro avg       0.98      0.98      0.98       590
weighted avg       0.98      0.98      0.98       590

Grad-CAM for the Brain tumor detector model¶

Below is an example from the grad-CAM visualisation applied to the brain tumor image classifier model.

No description has been provided for this image

In the grid below we see that the attention map is corresponding well with the clinicians mask - eventhough the classifier has never seen or beentrained on the mask.

This block loops over a set of test images (here: cases predicted as containing tumor), loading each MRI slice and its ground-truth mask (if available), and then computing a Grad-CAM attention map for the classifier’s decision. The Grad-CAM attention map is contrast-enhanced for visibility. I chose to look further “in” than the very last convolutional layer (via the robust layer selection) because the final conv features can become too coarse or gradient-saturated, producing weak or even blank maps; earlier convolutional layers often preserve more spatial detail and yield attention maps that align better with the actual tumor region, making the explanation more informative and easier to interpret.

Output hidden; open in https://colab.research.google.com to view.
/content/drive/MyDrive/Colab Notebooks/brain-tumor-detector/Brain_MRI
Exception ignored in: <function _xla_gc_callback at 0x7c04b9464900>
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/dist-packages/jax/_src/lib/__init__.py", line 127, in _xla_gc_callback
    def _xla_gc_callback(*args):
    
KeyboardInterrupt: 
Layer visualization
Output hidden; open in https://colab.research.google.com to view.
Output hidden; open in https://colab.research.google.com to view.
Grad-CAM